﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace TripLOAMobile
{
    /// <summary>
    /// An AddressPanel is auxiliary input control which it easier the filling
    /// of an <see cref="AddressBox"/>. 
    /// The AddressPanle must be used jointly to <see cref="AddressBox"/> class.
    /// </summary>
    public partial class AddressPanel : UserControl
    {
        private Image m_DoubleBuffer;
        private AddressBox m_FillingBox;

        /// <summary>
        /// Initializes a new instance of the <see cref="AddressPanel"/> class.
        /// </summary>
        public AddressPanel()
        {
            InitializeComponent();
            #region DoubleBufferInitialization
            Size cSz = ClientSize;
            m_DoubleBuffer = new Bitmap(cSz.Width, cSz.Height);
            FillDoubleBuffer();
            #endregion
        }

        /// <summary>
        /// Gets or sets the street label, useful for localization.
        /// </summary>
        /// <value>The street label.</value>
        public string StreetLabel { get { return lblStreet.Text; } set { lblStreet.Text = value; } }
        /// <summary>
        /// Gets or sets the zip label, useful for localization.
        /// </summary>
        /// <value>The zip label.</value>
        public string ZipLabel { get { return lblZip.Text; } set { lblZip.Text = value; } }
        /// <summary>
        /// Gets or sets the city label, useful for localization.
        /// </summary>
        /// <value>The city label.</value>
        public string CityLabel { get { return lblCity.Text; } set { lblCity.Text = value; } }
        /// <summary>
        /// Gets or sets the province label, useful for localization.
        /// </summary>
        /// <value>The province label.</value>
        public string ProvinceLabel { get { return lblProvince.Text; } set { lblProvince.Text = value; } }
        /// <summary>
        /// Gets or sets the nation label, useful for localization.
        /// </summary>
        /// <value>The nation label.</value>
        public string NationLabel { get { return lblNation.Text; } set { lblNation.Text = value; } }

        /// <summary>
        /// Gets or sets the confirm button label, useful for localization.
        /// </summary>
        /// <value>The confirm button label.</value>
        public string ConfirmBtnLabel { get { return btnOk.Text; } set { btnOk.Text = value; } }
        /// <summary>
        /// Gets or sets the cancel button label, useful for localization.
        /// </summary>
        /// <value>The cancel button label.</value>
        public string CancelBtnLabel { get { return btnCancel.Text; } set { btnCancel.Text = value; } }

        /// <summary>
        /// Gets or sets the filling <see cref="AddressBox"/>, which the control is related to.
        /// </summary>
        /// <value>The filling <see cref="AddressBox"/>.</value>
        public AddressBox FillingBox { get { return m_FillingBox; } set { m_FillingBox = value; } }
        
        #region View
        /// <summary>
        /// Raises the <see cref="E:System.Windows.Forms.Control.Paint"/> event.
        /// </summary>
        /// <param name="e">A <see cref="T:System.Windows.Forms.PaintEventArgs"/> that contains the event data.</param>
        protected override void OnPaint(PaintEventArgs e)
        {
            using(Graphics g = e.Graphics)
            {
                g.DrawImage(m_DoubleBuffer, 0, 0);
            }

            base.OnPaint(e);
        }

        /// <summary>
        /// Fills the double buffer, updating it.
        /// Because of the static aspect, this method can safely be invoked 
        /// only when a real change is applyed to the aspect.
        /// </summary>
        private void FillDoubleBuffer()
        {
            using (Graphics g = Graphics.FromImage(m_DoubleBuffer))
            {
                g.Clear(BackColor);
                using (Pen p = new Pen(Color.GreenYellow, 3))
                    g.DrawRectangle(p, 1, 1, Width - 4, Height - 4);
                g.DrawImage(Properties.Resources.circles, 202, 155);
            }
        }
        #endregion

        /// <summary>
        /// Handles the Click event of the btnCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Hide();
        }

        /// <summary>
        /// Handles the Click event of the btnOk control, by filling the associated <see cref="AddressBox"/>.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <exception cref="InvalidOperationException">Throws an exception if no <see cref="AddressPanel"/> is associated too.</exception>
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (m_FillingBox == null) throw new InvalidOperationException("No filling box is associated to the panel");
            
            m_FillingBox.UpdateAddress(
                fldStreet.Text,
                fldCity.Text, 
                fldZipcode.Text,
                fldProvince.Text,
                fldNation.Text);
            Hide();
        }

        /// <summary>
        /// Handles the GotFocus event of the txtBox control, by showing the SIP.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void txtBox_GotFocus(object sender, EventArgs e)
        {
            //Shows the INPUT_PANEL
            sip.Enabled = true;
        }

        /// <summary>
        /// Handles the LostFocus event of the txtBox control, by hiding the SIP.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void txtBox_LostFocus(object sender, EventArgs e)
        {
            //Hides the INPUT_PANEL
            sip.Enabled = false;
        }

        /// <summary>
        /// Handles the EnabledChanged event of the sip control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void sip_EnabledChanged(object sender, EventArgs e)
        {
            //Se occulta qualcosa, cambiare la presentazione affinché rimanga tutto visibile
        }
    }
}
